package org.example.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.dao.ExamPaperDao;
import org.example.dao.QuestionInfoDao;
import org.example.entity.ExamPaperEntity;
import org.example.entity.QuestionInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("ExamPaperService")
public class ExamPaperSaveServiceImpl extends ServiceImpl<ExamPaperDao, ExamPaperEntity> implements ExamPaperSaveService {

    @Autowired
    private ExamPaperDao examPaperDao;

    @Autowired
    private QuestionInfoDao questionInfoDao;

    @Override
    @Transactional
    public boolean saveExamPaperWithQuestions(ExamPaperEntity exampaper, List<QuestionInfoEntity> questions) {
        // 保存试卷信息
        int paperInserted = examPaperDao.insert(exampaper);
        if (paperInserted <= 0) {
            return false;
        }

        // 获取新插入的试卷ID
        Integer exampaperId = Math.toIntExact(exampaper.getExamId());

        // 保存题目信息
        for (QuestionInfoEntity question : questions) {
            question.setExamId(Math.toIntExact(exampaperId)); // 设置试卷ID
            questionInfoDao.insert(question);
        }

        return true;
    }
}

